import { Geometry } from "./Geometry";
import { all, any, has } from "../core/logic";
import { process } from "../process/worker/process";
/**
 * @class
 * @memberof w.geometry
 * @extends w.geometry.Geometry
 */
class SphereGeometry extends Geometry {
  /**
   * 球体构造方法
   * @param {*} opts
   * @param {Number} opts.radius 球体半径
   * @param {Number} opts.widthSegments 水平线段的数量
   * @param {Number} opts.heightSegments 垂直线段的数量
   * @param {Number} opts.phiStart 球体赤道线的起点弧度
   * @param {Number} opts.phiLength 球体赤道线的弧长
   * @param {Number} opts.thetaStart 球体子午线起点的弧度
   * @param {Number} opts.thetaLength 球面子午线弧长
   */
  constructor(opts = {}) {
    super(opts);
    this.type = "SphereGeometry";
    this.position = null;
    this._radius = any(opts.radius, 1);
    this._widthSegments = any(opts.widthSegments, 32);
    this._heightSegments = any(opts.heightSegments, 16);
    this._phiStart = any(opts.phiStart, 0);
    this._phiLength = any(opts.phiLength, Math.PI * 2);
    this._thetaStart = any(opts.thetaStart, 0);
    this._thetaLength = any(opts.thetaLength, Math.PI);
    this._back = any(opts.back, false);
    this.createSphere();
  }
  set back(v){
    this._back = v;
    return this;
  }
  get back(){
    return this._back;
  }
  set radius(v){
    this._radius = v;
    this.createSphere();
    return this;
  }
  get radius(){
    return this._radius;
  }
  set widthSegments(v){
    this._widthSegments = v;
    this.createSphere();
    return this;
  }
  get widthSegments(){
    return this._widthSegments;
  }
  set heightSegments(v){
    this._heightSegments = v;
    this.createSphere();
    return this;
  }
  get heightSegments(){
    return this._heightSegments;
  }
  set phiStart(v){
    this._phiStart = v;
    this.createSphere();
    return this;
  }
  get phiStart(){
    return this._phiStart;
  }
  set phiLength(v){
    this._phiLength = v;
    this.createSphere();
    return this;
  }
  get phiLength(){
    return this._phiLength;
  }
  set thetaStart(v){
    this._thetaStart = v;
    this.createSphere();
    return this;
  }
  get thetaStart(){
    return this._thetaStart;
  }
  set thetaLength(v){
    this._thetaLength = v;
    this.createSphere();
    return this;
  }
  get thetaLength(){
    return this._thetaLength;
  }

  createSphere() {
    if (all(has(true))) {
      let info = {
        back: this.back,
        radius: this.radius,
        widthSegments: this.widthSegments,
        heightSegments: this.heightSegments,
        phiStart: this.phiStart,
        phiLength: this.phiLength,
        thetaStart: this.thetaStart,
        thetaLength: this.thetaLength,
        type: "createSphere"
      }
      process(info, (message) => {
        let { position, indices, normal, texCood0 } = message.data;

        this.position = new Float32Array(position);
        this.normal = new Float32Array(normal);
        this.texCood0 = new Float32Array(texCood0);
        this.indices = new Uint32Array(indices);
      })
    }
  }
}

export { SphereGeometry }
